package com.luke.nio.channel;

import java.io.FileInputStream;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;

/**
 * 将文件内容读取出来
 * @Descrtption FileChannel
 * @Author luke
 * @Date 2020/4/23
 **/
public class NioFileChannel02 {

    public static void main(String[] args) throws Exception{
        String filePath = "E:\\netty\\a1.txt";
        FileInputStream fileInputStream = new FileInputStream(filePath);//文件输入流

        //获取FileInputStream 对应的 FileChannel
        FileChannel fileChannel = fileInputStream.getChannel();

        //将文件内容通过 FileChannel 写入到缓冲区 ByteBuffer
        ByteBuffer dstByteBuffer = ByteBuffer.allocate(fileInputStream.available());
        fileChannel.read(dstByteBuffer);

        //将缓冲区数据输出
        byte[] array = dstByteBuffer.array();
        System.out.println(new String(array));

        //关闭流
        fileInputStream.close();
    }

}
